home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / unix / tclUnixSock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  2.1 KB  |  99 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tclUnixSock.c --
  3.  *
  4.  *    This file contains Unix-specific socket related code.
  5.  *
  6.  * Copyright (c) 1995 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * SCCS: @(#) tclUnixSock.c 1.7 97/07/24 17:54:02
  12.  */
  13.  
  14. #include "tcl.h"
  15. #include "tclPort.h"
  16.  
  17. /*
  18.  * There is no portable macro for the maximum length
  19.  * of host names returned by gethostbyname().  We should only
  20.  * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
  21.  * host name limits.
  22.  *
  23.  * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
  24.  *
  25.  * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
  26.  * can return a fully qualified name from DNS of up to 255 bytes.
  27.  *
  28.  * Fix suggested by Viktor Dukhovni (viktor@esm.com)
  29.  */
  30.  
  31. #if defined(SYS_NMLN) && SYS_NMLEN >= 256
  32. #define TCL_HOSTNAME_LEN SYS_NMLEN
  33. #else
  34. #define TCL_HOSTNAME_LEN 256
  35. #endif
  36.  
  37.  
  38. /*
  39.  * The following variable holds the network name of this host.
  40.  */
  41.  
  42. static char hostname[TCL_HOSTNAME_LEN + 1];
  43. static int  hostnameInited = 0;
  44.  
  45. /*
  46.  *----------------------------------------------------------------------
  47.  *
  48.  * Tcl_GetHostName --
  49.  *
  50.  *    Get the network name for this machine, in a system dependent way.
  51.  *
  52.  * Results:
  53.  *    A string containing the network name for this machine, or
  54.  *    an empty string if we can't figure out the name.
  55.  *
  56.  * Side effects:
  57.  *    None.
  58.  *
  59.  *----------------------------------------------------------------------
  60.  */
  61.  
  62. char *
  63. Tcl_GetHostName()
  64. {
  65. #ifndef NO_UNAME
  66.     struct utsname u;
  67.     struct hostent *hp;
  68. #endif
  69.  
  70.     if (hostnameInited) {
  71.         return hostname;
  72.     }
  73.  
  74. #ifndef NO_UNAME
  75.     if (uname(&u) > -1) {
  76.         hp = gethostbyname(u.nodename);
  77.         if (hp != NULL) {
  78.             strcpy(hostname, hp->h_name);
  79.         } else {
  80.             strcpy(hostname, u.nodename);
  81.         }
  82.         hostnameInited = 1;
  83.         return hostname;
  84.     }
  85. #else
  86.     /*
  87.      * Uname doesn't exist; try gethostname instead.
  88.      */
  89.  
  90.     if (gethostname(hostname, sizeof(hostname)) > -1) {
  91.     hostnameInited = 1;
  92.         return hostname;
  93.     }
  94. #endif
  95.  
  96.     hostname[0] = 0;
  97.     return hostname;
  98. }
  99.